package com.handinfo.util;

/**
 * <p>
 * Title: Obm Game1
 * </p>
 * 
 * <p>
 * Description: Obm Game1
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * 
 * <p>
 * Company: Obm
 * </p>
 * 
 * @author Xia Mingwei
 * @version 1.0
 */
public class ArrayList
{
	public ArrayList()
	{
		m_elements_num_default = ELEMENTS_NUM_DEFAULT;
		m_elements_num_inc = ELEMENTS_NUM_INC;
		m_elements = new Object[m_elements_num_default];
	}

	public ArrayList(int num_default, int num_inc)
	{
		m_elements_num_default = num_default;
		m_elements_num_inc = num_inc;
		m_elements = new Object[m_elements_num_default];
	}

	/**
	 * 默认的容器的初始大小
	 */
	private static final int ELEMENTS_NUM_DEFAULT = 10;

	/**
	 * 默认的容器的动态增量
	 */
	private static final int ELEMENTS_NUM_INC = 10;

	/**
	 * 容器的初始大小
	 */
	private int m_elements_num_default;

	/**
	 * 容器的动态增量
	 */
	private int m_elements_num_inc;

	/**
	 * 容器的对象数组，容器内的所有数组均放在该数组内
	 */
	private Object[] m_elements;

	/**
	 * 容器内当前对象的个数
	 */
	private int m_elements_count;

	/**
	 * 向容器中添加对象
	 * 
	 * @param element
	 * Object
	 */
	public void addElement(Object o)
	{
		ensureCapacity(m_elements_count + 1);
		if (m_elements.length > m_elements_count) {
			m_elements[m_elements_count++] = o;
		}
	}

	/**
	 * 返回容器中首次出现指定对象o的索引
	 * 
	 * @param o
	 * Object
	 * @return int
	 */
	public int indexOf(Object o)
	{
		return indexOf(o, 0);
	}

	/**
	 * 返回容器中首次出现指定对象o的索引，从index开始搜索,如果没有指定对象o，返回-1
	 * 
	 * @param o
	 * Object
	 * @param index
	 * int
	 * @return int
	 */
	public int indexOf(Object o, int index)
	{
		if (index < 0) {
			throw new ArrayIndexOutOfBoundsException(index + " < 0");
		}
		if (o == null) {
			for (int i = index; i < m_elements_count; i++) {
				if (m_elements[i] == null)
					return i;
			}
		}
		else {
			for (int i = index; i < m_elements_count; i++) {
				if (m_elements[i].equals(o))
					return i;
			}
		}
		return -1;
	}

	public boolean contain(Object o)
	{
		return (indexOf(o) != -1);
	}

	/**
	 * 从容器中移除指定对象(如果有指定对象则返回true，没有则返回false)
	 * 
	 * @param element
	 * Object
	 */
	public boolean removeElememt(Object o)
	{
		int index = indexOf(o);
		if (index >= 0) {
			removeElementAt(index);
			return true;
		}
		return false;
	}

	/**
	 * 从容器中移除指定index的对象
	 * 
	 * @param index
	 * int
	 */
	public void removeElementAt(int index)
	{
		if (index >= m_elements_count || index < 0) {
			throw new ArrayIndexOutOfBoundsException(index + " >= "
					+ m_elements_count + " or " + index + " < 0");
		}
		int j = m_elements_count - index - 1;
		if (j > 0) {
			System.arraycopy(m_elements, index + 1, m_elements, index, j);
		}
		m_elements_count--;
		m_elements[m_elements_count] = null;
	}

	/**
	 * 移除容器内所有对象
	 */
	public void removeAllElements()
	{
		for (int i = 0; i < m_elements_count; i++) {
			m_elements[i] = null;
		}
		m_elements_count = 0;
	}

	/**
	 * 获取容器内第index个对象
	 * 
	 * @param index
	 * int
	 * @return Object
	 */
	public Object elementAt(int index)
	{
		if (index >= m_elements_count || index < 0) {
			throw new ArrayIndexOutOfBoundsException(index + " >= "
					+ m_elements_count + " or " + index + " < 0");
		}
		return m_elements[index];
	}

	/**
	 * 设置指定对象o到容器的指定索引处
	 * 
	 * @param o
	 * Object
	 * @param index
	 * int
	 */
	public void setElementAt(Object o, int index)
	{
		if (index >= m_elements_count || index < 0) {
			System.out.println("ERRIndex = " + index);
			throw new ArrayIndexOutOfBoundsException(index + " >= "
					+ m_elements_count + " or " + index + " < 0");
		}
		m_elements[index] = o;
	}

	/**
	 * 将指定对象o插入到容器的指定索引出
	 * 
	 * @param o
	 * @param index
	 */
	public void insertElementAt(Object o, int index)
	{
		ensureCapacity(m_elements_count + 1);
		for (int i = m_elements_count; i > index; i--) {
			m_elements[i] = m_elements[i - 1];
		}
		m_elements[index] = o;
		m_elements_count++;
	}

	/**
	 * 将容器中指定两个对象互换索引位
	 * 
	 * @param o1
	 * @param o2
	 */
	public void exchangeElement(Object o1, Object o2)
	{
		if (o1 == null || o2 == null || o1 == o2)
			return;
		int inx1 = indexOf(o1);
		int inx2 = indexOf(o2);
		if (inx1 == -1 || inx2 == -1)
			return;
		Object o = m_elements[inx1];
		m_elements[inx1] = m_elements[inx2];
		m_elements[inx2] = o;
	}

	/**
	 * 将容器中指定索引位的两个对象互换索引位
	 * 
	 * @param inx1
	 * @param inx2
	 */
	public void exchangeElement(int inx1, int inx2)
	{
		if (inx1 == -1 || inx1 > m_elements_count || inx2 == -1
				|| inx2 > m_elements_count) {
			return;
		}
		Object o = m_elements[inx1];
		m_elements[inx1] = m_elements[inx2];
		m_elements[inx2] = o;
	}

	/**
	 * 返回容器当前对象个数
	 * 
	 * @return int
	 */
	public int size()
	{
		return m_elements_count;
	}

	/**
	 * 返回容器当前大小
	 * 
	 * @return int
	 */
	public int capacity()
	{
		return m_elements.length;
	}

	/**
	 * 保证容器大小
	 * 
	 * @param num
	 * int
	 */
	private void ensureCapacity(int num)
	{
		if (num > m_elements.length) {
			Object[] tmp = new Object[m_elements.length + m_elements_num_inc];
			System.arraycopy(m_elements, 0, tmp, 0, m_elements.length);
			m_elements = tmp;
			tmp = null;
		}
	}

	/**
	 * 返回元素数组
	 * 
	 * @return
	 */
	public Object[] toArray()
	{
		Object[] elements = new Object[m_elements_count];
		System.arraycopy(m_elements, 0, elements, 0, m_elements_count);
		return elements;
	}
}
